/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screen;

import ClassFrame.Button;
import ClassFrame.CEditbox;
import ClassFrame.CFont;
import ClassFrame.InputKey;
import ClassFrame.Map;
import ClassFrame.Resource;
import ClassFrame.Utils;
import GamePlay.CanvasGame;
import Object.Answer;
import Object.Question;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author QuyetNM1
 */
public class Game extends IScreen {

    public static final int STATE_NONE = 0;
    public static final int STATE_EXIT = 1;
    public static final int STATE_TRUE = 2;
    public static final int STATE_FALSE = 3;
    public static final int STATE_TRALOI = 4;
    public static final int STATE_HETGIO = 5;
    public static final int STATE_STAGE_COMPLETE = 6;
    public static final int STATE_STAGE_FAIL = 7;
    public static final int STATE_STAGE_NOTENOUGH = 8;
    public static final int STATE_HINT = 9;
    public static final int MAX_TIME = 90;
    public static int MAX_LENGHT;
    public static int score = 0, scoreRound1 = 0, scoreRound2 = 0, scoreRound3 = 0;
    private static long startTime;
    private static long currentTime;
    private Button btnXacNhan, btnThoat;
//    private TextBox tb;
    private Question ques;
    private Answer answer;
    private CFont Font, fontBorder;
    public static int gameState = STATE_NONE;
    private Button btnOK, btnCo, btnKhong;
    public static int time;
    //test data
    private Image khung, anh, thongbao;
    String solution = "", hint = "";
    public static int idQuest = 0;
    private CEditbox answerText;

    public Game(CanvasGame _canvas, Question ques) {
        super(_canvas);
        this.ques = ques;
        if (ques != null) {
            solution = Utils.signToUnsign(ques.getAnswer().toLowerCase());
            MAX_LENGHT = solution.length();
            System.out.println(solution + MAX_LENGHT);
            hint = ques.getHint();
        }

    }

    public void initComponents() {
        btnThoat = new Button("Thoát", Resource.IMG_BTN_LT, 160, 290);
        btnXacNhan = new Button("Xác nhận", Resource.IMG_BTN_LT, 0, 290);

        btnOK = new Button("OK", Resource.IMG_BTN_LT, 80, 200);
        btnCo = new Button("Có", Resource.IMG_BTN_LT, 20, 180);
        btnKhong = new Button("Không", Resource.IMG_BTN_LT, 140, 180);

//        tb = new TextBox("Nhập câu trả lời của bạn", "", solution.length(), 0);
        answer = new Answer(canvas, 120, 253);
        gameState = STATE_NONE;

        if (Map.round != Map.ROUND3) {
            time = MAX_TIME;
        }

        String map = "0123456789.,:!?()+*<>/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ$";
        fontBorder = new CFont("/font/f.png",
                new byte[]{
                    7, 6, 7, 7, 7, 7, 7, 7, 7, 7,
                    4, 4, 4, 4, 8, 6, 6, 6, 5, 0, 8, 8, 8, 8, //.,:!?()+*|>/-%
                    7, 8, 6, 8, 7, 5, 8, 8, 4, 6,//abcdefghij
                    7, 4, 10, 8, 7, 7, 7, 6, 6, 5,//klmnopqrst
                    7, 7, 10, 7, 8, 6, 7, 7, 7, 7,//uvwxyzáàảã
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,//ạăắằẳẵặâấầ
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,//ẩẫậéèẻẽẹêế
                    8, 8, 8, 8, 5, 5, 5, 5, 5, 8,//ềểễệíìỉĩịóò
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7,
                    8, 8, 8, 8, 6, 6, 8, 8, 4, 6, //ABCDEFGHIJ
                    7, 6, 11, 9, 8, 8, 8, 8, 8, 7,//KLMNOPQRST
                    8, 8, 11, 8, 8, 7, 8, 8, 8,//UVWXYZĐ$Ă
                },
                13, 3, "0123456789.,:!?()+*|>/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ$Ă");


        Font = new CFont("/font/f_black.png",
                new byte[]{
                    7, 6, 7, 7, 7, 7, 7, 7, 7, 7,
                    4, 4, 4, 4, 8, 6, 6, 6, 5, 0, 8, 8, 8, 8, //.,:!?()+*|>/-%
                    7, 8, 6, 8, 7, 5, 8, 8, 4, 6,//abcdefghij
                    7, 4, 10, 8, 7, 7, 7, 6, 6, 5,//klmnopqrst
                    7, 7, 10, 7, 8, 6, 7, 7, 7, 7,//uvwxyzáàảã
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,//ạăắằẳẵặâấầ
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,//ẩẫậéèẻẽẹêế
                    8, 8, 8, 8, 5, 5, 5, 5, 5, 8,//ềểễệíìỉĩịóò
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7,
                    8, 8, 8, 8, 6, 6, 8, 8, 4, 6, //ABCDEFGHIJ
                    7, 6, 11, 9, 8, 8, 8, 8, 8, 7,//KLMNOPQRST
                    8, 8, 11, 8, 8, 7, 8, 8, 8,//UVWXYZĐ$Ă
                },
                13, 3, "0123456789.,:!?()+*|>/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ$Ă");


        try {
            khung = Image.createImage("/menu/khung.png");
            thongbao = Image.createImage("/menu/thongbao.png");
            if (ques != null) {
                anh = Image.createImage(ques.getPath());
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }


        answerText = new CEditbox(Font, 240 / 2 - 10, 320 / 2 - 50, 110, MAX_LENGHT);
        answerText.setEntryMode(CEditbox.ENTRY_ALPHANUMERIC);
        answerText.SetFocus(true);
    }

    public void load_screen() {
        canvas.getResource().loadArray(Resource.gameArrayImages);
    }

    public void un_load_screen() {
        canvas.getResource().unLoadArray(Resource.gameArrayImages);
    }

    public void keyEvent(int keycode, int event) {
        if (event == InputKey.KEY_EVENT_DOWN) {
            if (gameState == STATE_NONE) {
                getKeySpecial(keycode, event);
                if (keycode != '+' && keycode != '*') {
                    answerText.OnKeyDown(keycode);
                }
            }

            switch (keycode) {
                case InputKey.KEY_LEFT_SOFTKEY:
//                    canvas.getMIDletGame().getDisplay().setCurrent(tb);
//                    tb.addCommand(new Command("OK", Command.OK, 1));
//                    tb.setCommandListener(new CommandListener() {
//
//                        public void commandAction(Command c, Displayable d) {
//                            if (c.getCommandType() == Command.OK) {
//                                canvas.getMIDletGame().getDisplay().setCurrent(canvas);
//                            }
//                        }
//                    });
                    break;
                case InputKey.KEY_RIGHT_SOFTKEY:
                    canvas.setScreen(new Menu(canvas));
                    break;
                case InputKey.KEY_OK:
                    break;
            }
        } else if (event == InputKey.KEY_EVENT_UP) {
            if (gameState == STATE_NONE) {
                answerText.OnKeyUp(keycode);
            }
        }
    }
    public static final int xSound = 105;
    public static final int ySound = 290;

    public void pointerEvent(int x, int y, int event) {
        switch (event) {
            case InputKey.POINTER_EVENT_DOWN:
                switch (gameState) {
                    case STATE_NONE:
                        if (canvas.checkButton(x, y, btnThoat)) {
                            btnThoat.setState(Button.HOLD_CLICK);
                            canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                        } else if (canvas.checkButton(x, y, btnXacNhan)) {
                            btnXacNhan.setState(Button.HOLD_CLICK);
                            canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                        }
                        if (canvas.checkRegion(x, y, xSound, ySound, 30, 30)) {
                            CanvasGame.isSound = !CanvasGame.isSound;
                            if (CanvasGame.isSound) {
                                CanvasGame.musicLevel = 80;
                                canvas.getResource().sounds[Resource.SOUND_PLAY].setVolumeLevel(CanvasGame.musicLevel);
                            } else {
                                CanvasGame.musicLevel = 0;
                                canvas.getResource().sounds[Resource.SOUND_PLAY].setVolumeLevel(CanvasGame.musicLevel);
                            }
                        }

                        if (canvas.checkRegion(x, y, xBtnHint, yBtnHint, 42, 40) && Map.level >= 4) {
                            gameState = STATE_HINT;
                        }
                        break;
                    case STATE_EXIT:
                        if (canvas.checkButton(x, y, btnCo)) {
                            btnCo.setState(Button.HOLD_CLICK);
                            canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                        } else if (canvas.checkButton(x, y, btnKhong)) {
                            btnKhong.setState(Button.HOLD_CLICK);
                            canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                        }
                        break;
                    case STATE_TRALOI:
                        if (canvas.checkButton(x, y, btnCo)) {
                            btnCo.setState(Button.HOLD_CLICK);
//                            canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                        } else if (canvas.checkButton(x, y, btnKhong)) {
                            btnKhong.setState(Button.HOLD_CLICK);
//                            canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                        }
                        break;
                    case STATE_HETGIO:
                    case STATE_FALSE:
                    case STATE_TRUE:
                    case STATE_STAGE_COMPLETE:
                    case STATE_STAGE_FAIL:
                    case STATE_STAGE_NOTENOUGH:
                    case STATE_HINT:
                        if (canvas.checkButton(x, y, btnOK)) {
                            btnOK.setState(Button.HOLD_CLICK);
                            canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                        }
                        break;
                }
                break;
            case InputKey.POINTER_EVENT_UP:
                switch (gameState) {
                    case STATE_NONE:

                        if (canvas.checkButton(x, y, btnThoat) && btnThoat.getState() == Button.HOLD_CLICK) {
                            gameState = STATE_EXIT;
                        } else if (canvas.checkButton(x, y, btnXacNhan) && btnXacNhan.getState() == Button.HOLD_CLICK) {
                            gameState = STATE_TRALOI;
                        }
                        break;
                    case STATE_HINT:
                        if (canvas.checkButton(x, y, btnOK) && btnOK.getState() == Button.HOLD_CLICK) {
                            gameState = STATE_NONE;
                        }
                        break;
                    case STATE_EXIT:
                        if (canvas.checkButton(x, y, btnCo) && btnCo.getState() == Button.HOLD_CLICK) {
                            canvas.setScreen(new Menu(canvas));
                        } else if (canvas.checkButton(x, y, btnKhong) && btnKhong.getState() == Button.HOLD_CLICK) {
                            gameState = STATE_NONE;
                        }
                        break;
                    case STATE_FALSE:
                        if (canvas.checkButton(x, y, btnOK) && btnOK.getState() == Button.HOLD_CLICK) {
                            switch (Map.round) {
                                case Map.ROUND1:
                                    if (Map.piece == 10) {
                                        if (scoreRound1 < 800) {
                                            gameState = STATE_STAGE_FAIL;
                                        } else {
                                            gameState = STATE_STAGE_COMPLETE;
                                        }
                                    }
                                    if (gameState != STATE_STAGE_FAIL && gameState != STATE_STAGE_COMPLETE) {
                                        canvas.setScreen(new ChoosePiece(canvas));
                                    }
                                    break;
                                case Map.ROUND2:
                                    if (idQuest > 7) {
                                        if (scoreRound2 >= 1000) {
                                            gameState = STATE_STAGE_COMPLETE;
                                        } else {
                                            gameState = STATE_STAGE_FAIL;
                                        }
                                    } else {
                                        if (gameState != STATE_STAGE_FAIL && gameState != STATE_STAGE_COMPLETE) {
                                            idQuest++;
                                            canvas.setScreen(new Game(canvas, canvas.getMap().getQuestionByID(idQuest)));
                                        }
                                    }
                                    break;
                                case Map.ROUND3:
                                    if (gameState != STATE_STAGE_FAIL && gameState != STATE_STAGE_COMPLETE) {
                                        canvas.setScreen(new ChoosePiece(canvas));
                                    }
                                    break;
                            }
                        }
                        break;
                    case STATE_TRUE:
                        if (canvas.checkButton(x, y, btnOK) && btnOK.getState() == Button.HOLD_CLICK) {
                            if (ques != null) {
                                ques.setRight();
                            }
                            switch (Map.round) {
                                case Map.ROUND1:
                                    if (Map.piece != 10) {
                                        score += 100;
                                        scoreRound1 += 100;
                                        canvas.setScreen(new ChoosePiece(canvas));
                                    } else {
                                        score += 500;
                                        scoreRound1 += 500;
                                        if (scoreRound1 < 800) {
                                            gameState = STATE_STAGE_FAIL;
                                        } else {
                                            gameState = STATE_STAGE_COMPLETE;
                                        }
                                    }
                                    break;
                                case Map.ROUND2:
                                    score += 250 - 2 * (MAX_TIME - time);
                                    scoreRound2 += 250 - 2 * (MAX_TIME - time);

                                    if (idQuest > 7) {
                                        if (scoreRound2 >= 1000) {
//                                            Map.round = Map.ROUND3;
                                            gameState = STATE_STAGE_COMPLETE;
                                        } else {
                                            gameState = STATE_STAGE_FAIL;
                                        }
                                    } else {
                                        if (gameState != STATE_STAGE_FAIL && gameState != STATE_STAGE_COMPLETE) {
                                            idQuest++;
                                            System.out.println("id " + idQuest);
                                            canvas.setScreen(new Game(canvas, canvas.getMap().getQuestionByID(idQuest)));
                                        }
                                    }
                                    break;
                                case Map.ROUND3:
                                    if (gameState != STATE_STAGE_FAIL && gameState != STATE_STAGE_COMPLETE) {
                                        canvas.setScreen(new ChoosePiece(canvas));
                                    }
                                    break;
                            }
                        }
                        break;
                    case STATE_TRALOI:
                        if (canvas.checkButton(x, y, btnCo) && btnCo.getState() == Button.HOLD_CLICK) {
                            if (Utils.signToUnsign(answerText.getText()).trim().equals(solution.trim())) {
                                gameState = STATE_TRUE;
                                canvas.getResource().playSound(Resource.SOUND_CORRECT, 1);
                                if (Map.round == Map.ROUND3) {
                                    score += 300;
                                    scoreRound3 += 300;
                                }
                            } else {
                                gameState = STATE_FALSE;
                                if (Map.round == Map.ROUND3) {
                                    score -= 100;
                                    scoreRound3 -= 100;
                                }
                                canvas.getResource().playSound(Resource.SOUND_WRONG, 1);
                            }
                        } else if (canvas.checkButton(x, y, btnKhong) && btnKhong.getState() == Button.HOLD_CLICK) {
                            gameState = STATE_NONE;
                        }
                        break;
                    case STATE_HETGIO:
                        if (canvas.checkButton(x, y, btnOK) && btnOK.getState() == Button.HOLD_CLICK) {
                            if (Map.round == Map.ROUND3) {
                                canvas.getRMS().open();
                                canvas.getaPlayer().setScore(score);
                                canvas.getRMS().replaceWith(canvas.getaPlayer());
                                canvas.getRMS().close();
                                canvas.setScreen(new Menu(canvas));
                            } else if (Map.round == Map.ROUND2) {
                                if (idQuest > 7) {
                                    if (scoreRound2 >= 1000) {
                                        gameState = STATE_STAGE_COMPLETE;
                                    } else {
                                        gameState = STATE_STAGE_FAIL;
                                    }
                                } else {
                                    if (gameState != STATE_STAGE_FAIL && gameState != STATE_STAGE_COMPLETE) {
                                        idQuest++;
                                        canvas.setScreen(new Game(canvas, canvas.getMap().getQuestionByID(idQuest)));
                                    }
                                }
                            } else {
                                if (Map.piece != 10) {
                                    canvas.setScreen(new ChoosePiece(canvas));
                                } else {
                                    if (scoreRound1 < 800) {
                                        gameState = STATE_STAGE_FAIL;
                                    } else {
                                        gameState = STATE_STAGE_COMPLETE;
                                    }
                                }
                            }
                        }
                        break;
                    case STATE_STAGE_COMPLETE:
                        if (canvas.checkButton(x, y, btnOK) && btnOK.getState() == Button.HOLD_CLICK) {
                            if (Map.round == Map.ROUND3) {
                                canvas.getRMS().open();
                                canvas.getaPlayer().setScore(score);
                                canvas.getRMS().replaceWith(canvas.getaPlayer());
//                                if (Map.level < 3) {
                                if (Map.level == Map.levelMax) {
                                    canvas.getRMS().saveLevelMax(Map.levelMax + 1);
                                }
                                canvas.getRMS().close();
                                canvas.setScreen(new Menu(canvas));
                            } else {
                                Map.round++;
                                canvas.setScreen(new Rule(canvas));
                            }
                        }
                        break;
                    case STATE_STAGE_FAIL:
                        if (canvas.checkButton(x, y, btnOK) && btnOK.getState() == Button.HOLD_CLICK) {
                            canvas.getRMS().open();
                            canvas.getaPlayer().setScore(score);
                            canvas.getRMS().replaceWith(canvas.getaPlayer());
                            canvas.getRMS().close();
                            canvas.setScreen(new Menu(canvas));
                        }
                        break;
                }


                btnOK.setState(Button.ENABLE);
                btnCo.setState(Button.ENABLE);
                btnKhong.setState(Button.ENABLE);
                btnXacNhan.setState(Button.ENABLE);
                btnThoat.setState(Button.ENABLE);
                break;
        }
    }
    private int count = 0;

    public void update() {
    }
    public static final int xBtnHint = 181, yBtnHint = 27;

    public void paint(Graphics g) {
        count++;
        canvas.getImage().drawImage(g, canvas, Resource.IMG_BG_PLAY, 0, 0);
        if (ques != null) {
            g.drawImage(anh, 20, 80, Graphics.TOP | Graphics.LEFT);
            if (Map.level >= 4) {
                canvas.getImage().drawImagePart(g, canvas, Resource.IMG_HINT, xBtnHint, yBtnHint, 42 * (count % 8 / 2), 0, 42, 40);
            }
        }

        if (Map.piece == 10 && Map.round == Map.ROUND1) {
            for (int i = 0; i < canvas.getMap().getpArray().length; i++) {
                for (int j = 0; j < canvas.getMap().getpArray()[i].length; j++) {
                    if (canvas.getMap().getqArray()[i][j].isAnswered()) {
                        if (!canvas.getMap().getqArray()[i][j].isRight()) {
                            canvas.getImage().drawImagePart(g, canvas, Resource.IMG_PIECE2, 26 + 63 * i, 86 + 45 * j, 0, 90, 63, 45);
                        }
                    } else {
                        canvas.getImage().drawImagePart(g, canvas, Resource.IMG_PIECE2, 26 + 63 * i, 86 + 45 * j, 0, 45, 63, 45);
                    }
                }
            }
        }
        if (ques != null) {
            g.drawImage(khung, 16, 76, Graphics.TOP | Graphics.LEFT);
        }
        canvas.getImage().drawNumber(g, canvas, Resource.IMG_NUMBER, time, 88, 6, ClassFrame.Image.CENTER);
        canvas.getImage().drawNumber(g, canvas, Resource.IMG_NUMBER, score, 180, 6, ClassFrame.Image.LEFT);

        if (CanvasGame.isSound) {
            canvas.getImage().drawImagePart(g, canvas, Resource.IMG_BTN_SOUND_PLAY, xSound, ySound, 0, 0, 30, 30);
        } else {
            canvas.getImage().drawImagePart(g, canvas, Resource.IMG_BTN_SOUND_PLAY, xSound, ySound, 0, 30, 30, 30);
        }

        btnThoat.drawButton(g, canvas);
        btnXacNhan.drawButton(g, canvas);

        answer.drawAnswer(g, Utils.signToUnsign((answerText.getText()).toLowerCase()), solution, Font);
        switch (gameState) {
            case STATE_NONE:
                downClock();
                if (time <= 0) {
                    if (Map.round != Map.ROUND3) {
                        gameState = STATE_HETGIO;
                    } else {
                        if (scoreRound3 >= 1000) {
                            gameState = STATE_STAGE_COMPLETE;
                        } else {
                            gameState = STATE_STAGE_FAIL;
                        }
                    }
                }
                break;
            case STATE_HINT:
                downClock();
                if (time <= 0) {
                    if (Map.round != Map.ROUND3) {
                        gameState = STATE_HETGIO;
                    } else {
                        if (scoreRound3 >= 1000) {
                            gameState = STATE_STAGE_COMPLETE;
                        } else {
                            gameState = STATE_STAGE_FAIL;
                        }
                    }
                }
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                fontBorder.Render(g, Utils.segment(hint, 28), 120, 120, CFont.ALIGN_CENTER);
                btnOK.drawButton(g, canvas);
                break;
            case STATE_TRALOI:
                downClock();
                if (time <= 0) {
                    if (Map.round != Map.ROUND3) {
                        gameState = STATE_HETGIO;
                    } else {
                        if (scoreRound3 >= 1000) {
                            gameState = STATE_STAGE_COMPLETE;
                        } else {
                            gameState = STATE_STAGE_FAIL;
                        }
                    }
                }
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                fontBorder.Render(g, "Bạn có chắc chắn với câu trả lời\n này không?", 120, 130, CFont.ALIGN_CENTER);
                btnCo.drawButton(g, canvas);
                btnKhong.drawButton(g, canvas);
                break;
            case STATE_EXIT:
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                fontBorder.Render(g, "Bạn có chắc chắn muốn thoát?", 120, 130, CFont.ALIGN_CENTER);
                btnCo.drawButton(g, canvas);
                btnKhong.drawButton(g, canvas);
                break;
            case STATE_TRUE:
                if (Map.round == Map.ROUND3) {
                    downClock();
                    if (time <= 0) {
                        if (Map.round != Map.ROUND3) {
                            gameState = STATE_HETGIO;
                        } else {
                            if (scoreRound3 >= 1000) {
                                gameState = STATE_STAGE_COMPLETE;
                            } else {
                                gameState = STATE_STAGE_FAIL;
                            }
                        }
                    }
                }
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                fontBorder.Render(g, Utils.segment("Đúng rồi!!  " + ques.getAnswer() + " - " + hint, 28), 120, 120, CFont.ALIGN_CENTER);
                btnOK.drawButton(g, canvas);
                break;
            case STATE_FALSE:
                if (Map.round == Map.ROUND3) {
                    downClock();
                    if (time <= 0) {
                        if (Map.round != Map.ROUND3) {
                            gameState = STATE_HETGIO;
                        } else {
                            if (scoreRound3 >= 1000) {
                                gameState = STATE_STAGE_COMPLETE;
                            } else {
                                gameState = STATE_STAGE_FAIL;
                            }
                        }
                    }
                }
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                btnOK.drawButton(g, canvas);
                fontBorder.Render(g, "Rất tiếc, bạn đã trả lời sai", 120, 130, CFont.ALIGN_CENTER);
                break;
            case STATE_HETGIO:
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                btnOK.drawButton(g, canvas);
                if (Map.round == Map.ROUND3) {
                    fontBorder.Render(g, "Hết giờ!!!\n Rất tiếc, bạn đã không thể chiến \nthắng trong vòng thi thứ 3 \nnày. Tổng số điểm bạn ghi \nđược là " + String.valueOf(score) + " điểm!", 120, 110, CFont.ALIGN_CENTER);
                } else {
                    fontBorder.Render(g, "Hết giờ!!!\n Rất tiếc, bạn đã không ghi được \n điểm trong câu hỏi này", 120, 130, CFont.ALIGN_CENTER);
                }
                break;
            case STATE_STAGE_COMPLETE:
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                btnOK.drawButton(g, canvas);
                if (Map.round == Map.ROUND1 || Map.round == Map.ROUND2) {
                    fontBorder.Render(g, "Xin chúc mừng!!!\n Bạn đã vượt qua được vòng " + String.valueOf(Map.round + 1) + " với \ntổng số điểm đạt được là " + String.valueOf(score) + "\n Hãy cùng đến với vòng " + String.valueOf(Map.round + 2) + "!", 120, 115, CFont.ALIGN_CENTER);
                } else {
                    fontBorder.Render(g, "Xin chúc mừng!!!\n Bạn đã là người chiến thắng \ntrong chương trình với tổng \nsố điểm đạt được là " + String.valueOf(score) + "\nXin cám ơn bạn đã tham \ngia chương trình!", 120, 105, CFont.ALIGN_CENTER);
                }
                break;
            case STATE_STAGE_FAIL:
                g.drawImage(thongbao, 10, 90, Graphics.TOP | Graphics.LEFT);
                btnOK.drawButton(g, canvas);
                fontBorder.Render(g, "Rất tiếc, bạn đã không đủ\n điểm để qua được vòng thi này !\n Số điểm bạn đạt được là " + String.valueOf(score) + "\nChúc bạn may mắn lần sau !", 120, 115, CFont.ALIGN_CENTER);
                break;
        }

    }

    public boolean isAnswerAll() {
        for (int i = 0; i < canvas.getMap().getpArray().length; i++) {
            for (int j = 0; j < canvas.getMap().getpArray()[i].length; j++) {
                if (!canvas.getMap().getqArray()[i][j].isAnswered()) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void downClock() {

        currentTime = System.currentTimeMillis();
        if (currentTime - startTime >= 1000) {
            startTime = System.currentTimeMillis();
            currentTime = startTime;
            if (time >= 1) {
                time--;
            }
        }
    }
    String input = "";

    public void addChar(final char c) {
        if (input.length() < MAX_LENGHT) {
            if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == ' ') {
                input = input + String.valueOf(c);
            }
        }
    }

    public void removeChar() {
        if (input.length() > 0) {
            if (input.length() > 1) {
                input = input.substring(0, input.length() - 1);
            } else {
                input = "";
            }
        }
    }

    public void removeCharEditBox() {
        String inputEB = answerText.getText();
        if (inputEB.length() > 0) {
            if (inputEB.length() > 1) {
                answerText.setText(inputEB.substring(0, inputEB.length() - 1));
            } else {
                answerText.setText("");
            }
        }
    }

    public void getKey(int keycode, int event) {
        if (event == InputKey.KEY_EVENT_DOWN) {
            if (keycode == 8 || keycode == -8) { //key clear
                removeChar();
            } else {
                addChar((char) keycode);
            }
        }
    }

    public void getKeySpecial(int keycode, int event) {
        if (event == InputKey.KEY_EVENT_DOWN) {
            if (keycode == '+' || keycode == '*') { //key clear
                removeCharEditBox();
            }
        }
    }
}
